package com.face.pay;

import com.aliyun.oss.sdk.OSSUtils;
import com.face.pay.util.SimpleBeanUtil;
import com.github.wxpay.sdk.IWXPayDomain;
import com.github.wxpay.sdk.WXPayConfig;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import lombok.extern.slf4j.Slf4j;

import java.io.*;
import java.util.concurrent.TimeUnit;

/**
 * @Auther: zkj
 * @Date: 2019/8/26 15:50
 * @Description:
 */
@Slf4j
public class WxApiConfig extends WXPayConfig {

    private final static Cache<Object, byte[]> escCache = CacheBuilder.newBuilder()
            //设置cache的初始大小为10，要合理设置该值
            .initialCapacity(10)
            //设置并发数为5，即同一时间最多只能有5个线程往cache执行写入操作
            .concurrencyLevel(20)
            //设置cache中的数据在写入之后的存活时间为1个小时
            .expireAfterWrite(60 * 60, TimeUnit.SECONDS)
            //
            .maximumSize(10000)
            //构建cache实例
            .build();

    private SecurityConfig securityConfig;

    private String oemType;


    private String message = "";


    @Override
    public String getAppID() {

        return securityConfig.getAppId();
    }

    @Override
    public String getMchID() {
        return securityConfig.getMchId();
    }

    @Override
    public String getKey() {
        return securityConfig.getApiKey();
    }

    @Override
    public InputStream getCertStream() {
        String appId = getAppID();

        log.info("调用证书");
        try{
            byte[] secBytes = escCache.asMap().get(appId);
            log.info("获取证书内容");

            if(secBytes==null){
                log.info("缓存失效 远程获取证书内容");


                secBytes = OSSUtils.getOSSBytes(securityConfig.getEndPoint(),
                        securityConfig.getAccessKeyId(),
                        securityConfig.getAccessKeySecret(),
                        securityConfig.getBucket(),
                        securityConfig.getApiclientCertP12()
                );

                log.info("获取证书内容secBytes");

                escCache.put(appId,secBytes);

            }

            InputStream secInputStream = new ByteArrayInputStream(secBytes);
            return  secInputStream;
        }catch (Exception e){
            log.error("调用证书异常",e);
        }
        return null;
    }

    @Override
    public IWXPayDomain getWXPayDomain() {
        return null;
    }


    public SecurityConfig getSecurityConfig() {
        return securityConfig;
    }

    public void setSecurityConfig(SecurityConfig securityConfig) {
        this.securityConfig = securityConfig;
    }




    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }


    public Boolean validate(){

        if(this.securityConfig==null){
            return  false;
        }else {

           return SimpleBeanUtil.allNotEmpity(this.securityConfig);
        }


    }

    public String getOemType() {
        return oemType;
    }

    public void setOemType(String oemType) {
        this.oemType = oemType;
    }
}
